package com.situ.mall.thread.juc;



import org.junit.jupiter.api.Test;


import java.util.concurrent.atomic.AtomicInteger;

public class CASDemo {
    public static void main(String[] args) {
        AtomicInteger atomicInteger = new AtomicInteger(2020);
        boolean bool1 = atomicInteger.compareAndSet(2020, 2021);
        System.out.println(bool1);
        System.out.println(atomicInteger.get());//2021
        //如果和期望的值相同，就更新这个值，否则就不更新
        System.out.println(atomicInteger.compareAndSet(2020, 2022));
        System.out.println(atomicInteger.get());
    }

    @Test
    public void test1() {
        AtomicInteger atomicInteger = new AtomicInteger(0);
        System.out.println(atomicInteger.getAndIncrement());//i++  0
        System.out.println(atomicInteger.incrementAndGet());//++i  2

    }
}
